home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / BndNSUtl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  15.1 KB  |  546 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        BndNSUtl.cpp
  3.  
  4.     Contains:    Binding Namespace Utilities
  5.  
  6.     Owned by:    Reggie Adkins
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <6>     7/16/96    RA        Compile stand alone
  13.          <5>     6/27/96    TJ        Added GetgBinding to enable others to get
  14.                                     the binding session.
  15.          <4>     6/24/96    RA        T10025: Adjustments for CORBA compliant
  16.                                     API.
  17.          <3>     6/21/96    RA        T10025: Deprecated most utils for
  18.                                     implementors, obsoleted most for parts
  19.          <2>     5/23/96    RA        1330687: BndNSUtl should use
  20.                                     platformFile->read instead of FSRead
  21.  
  22.     To Do:
  23. */
  24.  
  25. #ifndef _BNDNSUTL_
  26. #include "BndNSUtl.h"
  27. #endif
  28.  
  29. #ifndef _EDITRSET_
  30. #include "EditrSet.h"
  31. #endif
  32.  
  33. #ifndef _ODDEBUG_
  34. #include "ODDebug.h"
  35. #endif
  36.  
  37. #ifndef _ODMEMORY_
  38. #include "ODMemory.h"
  39. #endif
  40.  
  41. #ifndef _STORAGEU_
  42. #include "StorageU.xh"
  43. #endif
  44.  
  45. #ifndef _STDDEFS_
  46. #include "StdDefs.xh"
  47. #endif
  48.  
  49. #ifndef _STDPROPS_
  50. #include "StdProps.xh"
  51. #endif
  52.  
  53. #ifndef _STDTYPES_
  54. #include "StdTypes.xh"
  55. #endif
  56.  
  57. #ifndef _PART_
  58. #include "Part.xh"
  59. #endif
  60.  
  61. #ifndef SOM_ODValueIterator_xh
  62. #include <ValueItr.xh>
  63. #endif
  64.  
  65. #ifndef SOM_ODObjectIterator_xh
  66. #include <ObjctItr.xh>
  67. #endif
  68.  
  69. #ifndef _ISOSTR_
  70. #include "ISOStr.h"
  71. #endif
  72.  
  73. #ifndef SOM_ODStorageUnitView_xh
  74. #include <SUView.xh>
  75. #endif
  76.  
  77. #ifndef _EXCEPT_
  78. #include "Except.h"
  79. #endif
  80.  
  81. #ifndef _ODUTILS_
  82. #include <ODUtils.h>
  83. #endif
  84.  
  85. //#ifndef SOM_ODNameSpaceManager_xh
  86. //#include <NmSpcMg.xh>
  87. //#endif
  88.  
  89. #ifndef SOM_ODTypeList_xh
  90. #include <TypeList.xh>
  91. #endif
  92.  
  93. #ifndef _STORUTIL_
  94. #include <StorUtil.h>
  95. #endif
  96.  
  97. #ifndef _UTILERRS_
  98. #include <UtilErrs.h>
  99. #endif
  100.  
  101. #ifndef SOM_ODTypeListIterator_xh
  102. #include <TypLsItr.xh>
  103. #endif
  104.  
  105. #ifndef SOM_ODBinding_xh
  106. #include <ODBindng.xh>
  107. #endif
  108.  
  109. #ifndef SOM_ODSession_xh
  110. #include <ODSessn.xh>
  111. #endif
  112.  
  113. #ifndef SOM_ODStorageSystem_xh
  114. #include <ODStor.xh>
  115. #endif
  116.  
  117. #include <somcls.xh>
  118. #include <somcm.xh>
  119.  
  120. ODStatic ODBinding*    gBinding = kODNULL;
  121. ODStatic ODSession*    gSession = kODNULL;
  122.  
  123. //------------------------------------------------------------------------------
  124. // InitBindingNamespaceUtils
  125. //------------------------------------------------------------------------------
  126. // New in OD 1.1, you must call this before calling any of the routines
  127. // below that take an ODNameSpaceManager* as a param.
  128. // These routines have been reimplemented as methods on the ODBinding object.
  129. // The routines below are deprecated in OD 1.1. Their new implementation will
  130. // work but you should really be calling the new ODBinding API's directly.
  131. // If you want to use these depricated routines to afford a degree of source
  132. // compatibility, you must add a call to InitBindingNamespaceUtils before 
  133. // making any calls to these utils.
  134.  
  135. void InitBindingNamespaceUtils(ODSession* session)
  136. {
  137.     ASSERT_NOT_NULL(session);
  138.     
  139.     Environment* ev = somGetGlobalEnvironment();
  140.     
  141.     gSession = session;
  142.     gBinding = session->GetBinding(ev);
  143. }
  144.  
  145. ODBinding* GetgBinding()
  146. {
  147.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  148.  
  149.     return gBinding;
  150. }
  151. #ifdef _OD_IMPL_
  152.  
  153. //------------------------------------------------------------------------------
  154. // GetSysPrefEditorForKind
  155. //------------------------------------------------------------------------------
  156. // Returns the system-wide preferred editor for the argument data kind.
  157. // A null value is returned if no preference has been specified.
  158. // This routine checks to ensure that the preferred editor actually exists;
  159. // if it does not, the preference is canceled and null is returned.
  160.  
  161. ODEditor GetSysPrefEditorForKind(ODNameSpaceManager* theNmSpcMgr, 
  162.                                     ODType kind)
  163. {
  164.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  165.  
  166.     Environment* ev = somGetGlobalEnvironment();
  167.     
  168.     return gBinding->GetSysPrefEditorForKind(ev, (ODKind)kind);
  169. }
  170.  
  171. //------------------------------------------------------------------------------
  172. // GetSysPrefEditorForCategory
  173. //------------------------------------------------------------------------------
  174. ODEditor GetSysPrefEditorForCategory(ODNameSpaceManager* theNmSpcMgr, 
  175.                                         ODType category)
  176. {
  177.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  178.  
  179.     Environment* ev = somGetGlobalEnvironment();
  180.     
  181.     return gBinding->GetSysPrefEditorForCategory(ev, category);
  182. }
  183.  
  184. //------------------------------------------------------------------------------
  185. // SetSysPrefEditorForKind
  186. //------------------------------------------------------------------------------
  187. ODEditor SetSysPrefEditorForKind(ODNameSpaceManager* theNmSpcMgr, 
  188.                                     ODType kind, ODEditor editorString)
  189. {
  190.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  191.  
  192.     Environment* ev = somGetGlobalEnvironment();
  193.  
  194.     return gBinding->SetSysPrefEditorForKind(ev, editorString, kind);
  195. }
  196.  
  197. //------------------------------------------------------------------------------
  198. // SetSysPrefEditorForCategory
  199. //------------------------------------------------------------------------------
  200. ODEditor SetSysPrefEditorForCategory(ODNameSpaceManager* theNmSpcMgr, 
  201.                                     ODType category,ODEditor editorString)
  202. {    
  203.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  204.  
  205.     Environment* ev = somGetGlobalEnvironment();
  206.  
  207.     return gBinding->SetSysPrefEditorForCategory(ev, editorString, category);
  208. }
  209.  
  210. //------------------------------------------------------------------------------
  211. // GetCategoriesOfKind
  212. //
  213. //     lookup kind in kindcategory namespace and return categories supported
  214. //------------------------------------------------------------------------------
  215.  
  216. ODTypeList* GetCategoriesOfKind(ODNameSpaceManager* theNmSpcMgr, 
  217.                                     ODType kind)
  218. {
  219.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  220.  
  221.     Environment* ev = somGetGlobalEnvironment();
  222.     
  223.     return gBinding->GetAllCategoriesForKind(ev, kind);
  224. }
  225.  
  226. #endif
  227.  
  228. //------------------------------------------------------------------------------
  229. // GetUserKindFromKind
  230. //------------------------------------------------------------------------------
  231. ODBoolean GetUserKindFromKind( ODNameSpaceManager* theNmSpcMgr, 
  232.                                     ODType kind, ODName** name )
  233. {
  234.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  235.  
  236.     Environment* ev = somGetGlobalEnvironment();
  237.     
  238.     ODName* userString = new ODName;
  239.     
  240.     ODBoolean result = gBinding->GetUserStringForKind(ev, kind, userString);
  241.     
  242.     *name = userString;
  243.         
  244.     return result;
  245. }
  246.  
  247.  
  248. //------------------------------------------------------------------------------
  249. // GetUserEditorFromEditor
  250. //------------------------------------------------------------------------------
  251. ODBoolean GetUserEditorFromEditor( ODNameSpaceManager* theNmSpcMgr, 
  252.                                     ODEditor editor, ODName** name )
  253. {
  254.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  255.  
  256.     Environment* ev = somGetGlobalEnvironment();
  257.     
  258.     ODName* userString = new ODName;
  259.     
  260.     ODBoolean result = gBinding->GetUserStringForEditor(ev, editor, userString);
  261.     
  262.     *name = userString;
  263.         
  264.     return result;
  265. }
  266.  
  267.  
  268. //------------------------------------------------------------------------------
  269. // GetUserCatFromCat
  270. //------------------------------------------------------------------------------
  271. ODBoolean GetUserCatFromCat( ODNameSpaceManager* theNmSpcMgr, 
  272.                                     ODType category, ODName** name )
  273. {
  274.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  275.  
  276.     Environment* ev = somGetGlobalEnvironment();
  277.     
  278.     ODName* userString = new ODName;
  279.     
  280.     ODBoolean result =  gBinding->GetUserStringForCategory(ev, category, userString);
  281.     
  282.     *name = userString;
  283.         
  284.     return result;
  285. }
  286.  
  287. #ifdef _OD_IMPL_
  288.  
  289. //------------------------------------------------------------------------------
  290. // ODGetODOSTypeForKind
  291. //------------------------------------------------------------------------------
  292. ODOSType ODGetODOSTypeForKind(ODNameSpaceManager* theNmSpcMgr, 
  293.                                         ODType kind)
  294. {
  295.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  296.  
  297.     Environment* ev = somGetGlobalEnvironment();
  298.     
  299.     return gBinding->GetODOSTypeForKind(ev,kind);
  300. }
  301.  
  302. //------------------------------------------------------------------------------
  303. // EditorSupportsKind
  304. //------------------------------------------------------------------------------
  305. ODBoolean EditorSupportsKind( ODNameSpaceManager* theNmSpcMgr,
  306.                                 ODEditor editor, ODType kind )
  307. {
  308.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  309.  
  310.     Environment* ev = somGetGlobalEnvironment();
  311.     
  312.     return gBinding->EditorSupportsKind(ev,editor,kind);
  313. }
  314.  
  315.  
  316. //------------------------------------------------------------------------------
  317. // GetAllEditorsForCategory
  318. //
  319. //     lookup kinds in editorkinds namespace and return editors supported
  320. //------------------------------------------------------------------------------
  321.  
  322. ODBoolean GetAllEditorsForCategory( ODNameSpaceManager* theNmSpcMgr,
  323.                                 ODType category, EditorSet* theEditorSet )
  324. {
  325.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  326.  
  327.     Environment*     ev             = somGetGlobalEnvironment();
  328.     ODTypeList*     editorList    = gSession->GetStorageSystem(ev)->CreateTypeList(ev,kODNULL);
  329.     ODBoolean         retVal        = kODFalse;
  330.     
  331.     retVal = gBinding->GetAllEditorsForCategory(ev,category,editorList);
  332.     
  333.     //For each element in editorList, add to theEditorSet for compatibility
  334.     if (editorList)
  335.     {
  336.         ODTypeListIterator* iter = editorList->CreateTypeListIterator(ev) ;
  337.         for (ODEditor editor = iter->First(ev);
  338.                     iter->IsNotComplete(ev);
  339.                     editor = iter->Next(ev))
  340.         {
  341.             theEditorSet->AddEditor(editor);
  342.             retVal = kODTrue;
  343.         }
  344.         ODDeleteObject( iter );
  345.     }
  346.     ODDeleteObject( editorList );
  347.  
  348.     return retVal;
  349. }
  350.  
  351.  
  352. //------------------------------------------------------------------------------
  353. // GetAllEditorsForKind
  354. //
  355. //     lookup kinds in editorkinds namespace and return editors supported
  356. //------------------------------------------------------------------------------
  357.  
  358. ODBoolean GetAllEditorsForKind(ODNameSpaceManager* theNmSpcMgr,
  359.                                 ODType kind, EditorSet* theEditorSet)
  360. {
  361.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  362.  
  363.     Environment*     ev             = somGetGlobalEnvironment();
  364.     ODTypeList*     editorList    = gSession->GetStorageSystem(ev)->CreateTypeList(ev,kODNULL);
  365.     ODBoolean         retVal        = kODFalse;
  366.  
  367.     retVal = gBinding->GetAllEditorsForKind(ev,kind,editorList);
  368.     
  369.     //For each element in editorList, add to theEditorSet for compatibility
  370.     if (editorList)
  371.     {
  372.             //    iterate through editorKinds typelist
  373.         ODTypeListIterator* iter = editorList->CreateTypeListIterator(ev) ;
  374.         for (ODEditor editor = iter->First(ev);
  375.                     iter->IsNotComplete(ev);
  376.                     editor = iter->Next(ev))
  377.         {
  378.             theEditorSet->AddEditor(editor);
  379.             retVal = kODTrue;
  380.         }
  381.     
  382.         ODDeleteObject( iter );
  383.     }
  384.     ODDeleteObject( editorList );
  385.  
  386.     return retVal;
  387. }
  388.  
  389. //------------------------------------------------------------------------------
  390. // GetAllKindsForEditor
  391. //------------------------------------------------------------------------------
  392.  
  393. void GetAllKindsForEditor(ODNameSpaceManager* theNmSpcMgr,
  394.                                 ODTypeList*    kindsList, ODEditor editor)
  395. {
  396.     ASSERTM(gBinding, kODErrAssertionFailed, "Need to call InitBindingNamespaceUtils first");
  397.  
  398.     Environment* ev = somGetGlobalEnvironment();
  399.     
  400.     gBinding->GetAllKindsForEditor(ev,editor,kindsList);
  401. }
  402.  
  403. //------------------------------------------------------------------------------
  404. // ReadSize
  405. //------------------------------------------------------------------------------
  406.  
  407. ODULong  ReadSize(PlatformFile* file, ODStorageUnitView* view)
  408. {
  409.     ODULong data;
  410.     ODSLong length = sizeof(data);
  411.     ODSLong savedLength = length;
  412.     Environment* ev = somGetGlobalEnvironment ();
  413.  
  414.     if (view)
  415.         length = StorageUnitViewGetValue(view, ev, length, &data);
  416.     else
  417.         file->Read(&data, &length);
  418.  
  419.     if (savedLength != length)
  420.         THROW(kODErrReadErr);
  421.  
  422.     return data;
  423. }
  424.  
  425. //------------------------------------------------------------------------------
  426. // ReadBytes
  427. //------------------------------------------------------------------------------
  428.  
  429. void ReadBytes(PlatformFile* file, ODStorageUnitView* view,
  430.                         ODPtr bytes, ODULong* length)
  431. {
  432.     ODSLong savedLength = *length;
  433.     Environment* ev = somGetGlobalEnvironment ();
  434.     
  435.     if (view)
  436.         *length = StorageUnitViewGetValue(view, ev, *length, bytes);
  437.     else
  438.         file->Read(bytes, (long*)length);
  439.  
  440.     if (savedLength != (ODSLong)*length)
  441.         THROW(kODErrReadErr);
  442. }
  443.  
  444. #endif
  445.  
  446. //------------------------------------------------------------------------------
  447. // ODGetEditorSetFromResFile
  448. //------------------------------------------------------------------------------
  449.  
  450. EditorSet* ODGetEditorSetFromResFile(ODSShort resRefNum)
  451. {
  452.     EditorSet*     theEditorSet = kODNULL;
  453.     ODBoolean    editorFound = kODFalse;
  454.     
  455.     if( resRefNum != -1 ) {
  456.         TRY{
  457.             LOG("* Reading 'nmap's from file %hd\n",resRefNum);
  458.             ODSShort nmapCount = Count1Resources( kODNameMappings );
  459.             for (short i=1; i<=nmapCount; i++) 
  460.             {
  461.                     // Get the Resource
  462.                 ODHandle nmap = (ODHandle)Get1IndResource(kODNameMappings, i);
  463.                 ODError result = ResError();
  464.                 
  465.                 if ( nmap != nil && !result  )
  466.                 {
  467.                     ODLockHandle((ODHandle) nmap);
  468.  
  469.                     theEditorSet = ODGetEditorSetFromNMAP(nmap);
  470.                                              
  471.                     ReleaseResource( nmap );
  472.                 }
  473.                 
  474.                 if (theEditorSet)
  475.                     break;
  476.             }
  477.         }CATCH_ALL{
  478.             WARN("Error %ld reading nmaps for file %hd",ErrorCode(),resRefNum);
  479.         }ENDTRY
  480.     }
  481.     return theEditorSet;
  482. }
  483.  
  484. //------------------------------------------------------------------------------
  485. // ODGetEditorSetFromNMAP
  486. //------------------------------------------------------------------------------
  487.  
  488. EditorSet* ODGetEditorSetFromNMAP( ODHandle nmap )
  489. {
  490.     ODISOStr        nameSpaceName = kODNULL; ODVolatile(nameSpaceName);
  491.     ODUShort        nameLen;
  492.     ODISOStr        editor;
  493.     ODUShort        editorLen;
  494.     ODSize            bufferPos=(ODSize)*nmap;
  495.     ODUShort        editorCount;
  496.     ODSize            sizeShort = sizeof(ODUShort);
  497.     Environment*     ev = somGetGlobalEnvironment ();
  498.     EditorSet*         theEditorSet = kODNULL;
  499.     
  500.     ODBlockMove((ODPtr)bufferPos, (ODPtr)&nameLen, sizeShort);
  501.     bufferPos += sizeShort;
  502.  
  503.     TRY
  504.         nameSpaceName = (ODISOStr)ODNewPtrClear((ODSize)nameLen+1, kDefaultHeapID);
  505.         ODBlockMove((ODPtr)bufferPos, (ODPtr)nameSpaceName, (ODSize)nameLen);
  506.         bufferPos += (ODSize)nameLen;
  507.         
  508.         if (ODISOStrEqual( nameSpaceName, kODEditorUserString ))
  509.         {
  510.             ODBlockMove((ODPtr)bufferPos, (ODPtr)&editorCount, sizeShort);
  511.             bufferPos += sizeShort;
  512.             
  513.             if ( editorCount > 0 )
  514.             {
  515.                 theEditorSet = new EditorSet() ;
  516.                 theEditorSet->InitEditorSet();
  517.     
  518.                 for(ODUShort i=0; i < editorCount; i++)
  519.                 {
  520.                     ODBlockMove((ODPtr)bufferPos, (ODPtr)&editorLen, sizeShort);
  521.                     bufferPos += sizeShort;
  522.             
  523.                     editor = (ODISOStr)ODNewPtrClear((ODSize)editorLen+1, kDefaultHeapID);
  524.                     ODBlockMove((ODPtr)bufferPos, (ODPtr)editor, (ODSize)editorLen);
  525.                     bufferPos += (ODSize)editorLen;
  526.                     
  527.                     theEditorSet->AddEditor(editor);
  528.                     
  529.                         // skip over the corresponding UserString in the nmap
  530.                     bufferPos += sizeShort;
  531.                     bufferPos += sizeof(ScriptCode);
  532.                     bufferPos += sizeof(LangCode);
  533.                     ODBlockMove((ODPtr)bufferPos, (ODPtr)&nameLen, sizeShort);
  534.                     bufferPos += sizeShort;
  535.                     bufferPos += nameLen;
  536.                 }
  537.             }
  538.         }
  539.     CATCH_ALL
  540.         WARN("Error %ld in Read_nmap_buffer",ErrorCode());
  541.     ENDTRY
  542.     ODDisposePtr((ODPtr)nameSpaceName);
  543.     return theEditorSet;
  544. }
  545.  
  546.